package pr.iceworld.fernando.commonbase.result;

/**
 * @author fightin
 * @since 2017-07-08 11:23
 */
public class WebBaseResult<T> {
    public static final int STATUS_SUCCESS = 200;//成功
    public static final int STATUS_FAIL = 500;//失败
    public static final int STATUS_LOGIN_USER_NOT_EXIST = 5001;//登录用户不存在
    public static final int STATUS_LOGIN_USER_PASSWORD_ERROR = 5002;//登录密码错误
    public static final int STATUS_REGIST_USERNAME_EXIST = 5010;//注册用户已存在

    protected int status;
    protected String msg;
    protected T data;

    public WebBaseResult() {

    }

    public WebBaseResult(T data) {
        this.data = data;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> WebBaseResult<T> success(T t) {
        WebBaseResult webBaseResult = new WebBaseResult<T>(t);
        webBaseResult.setStatus(STATUS_SUCCESS);
        return webBaseResult;
    }

    public static WebBaseResult success() {
//        WebBaseResult webBaseResult = new WebBaseResult();
//        webBaseResult.setStatus(STATUS_SUCCESS);
        return result(STATUS_SUCCESS);
    }

    public static <T> WebBaseResult<T> failure(String msg) {
//        WebBaseResult webBaseResult = new WebBaseResult<T>();
//        webBaseResult.setStatus(STATUS_FAIL);
//        webBaseResult.setMsg(msg);
        return result(STATUS_FAIL, msg);
    }

    private static <T> WebBaseResult<T> result(int status) {
//        WebBaseResult webBaseResult = new WebBaseResult<T>();
//        webBaseResult.setStatus(status);
        return result(status, null);
    }

    private static <T> WebBaseResult<T> result(int status, String msg) {
        WebBaseResult webBaseResult = new WebBaseResult<T>();
        webBaseResult.setStatus(status);
        webBaseResult.setMsg(msg);
        return webBaseResult;
    }

}
